package com.example.final_project;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioGroup;

import com.example.final_project.Cate.frag.CateFragment;
import com.example.final_project.Comm.frag.CommFragment;
import com.example.final_project.Home.frag.HomeOutFragment;
import com.example.final_project.ShoppingCar.frag.ShoppingCarFragment;
import com.example.final_project.Usr.frag.UsrFragment;

import java.util.ArrayList;
import java.util.List;

public class MainView extends AppCompatActivity {

    private RadioGroup radioGroup;
    private List<Fragment> frags;
    private int fragChose;
    private int nowFrag = -1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_view);

        radioGroup = findViewById(R.id.home_rg);

        //获得fragment
        frags = initfrag();
        selectHome();
        //设置默认显示首页
        radioGroup.check(R.id.home_rd);
        //登录页面点击取消后跳转到userfragment中去
        Intent intent = this.getIntent();
        if(intent !=null){
            String action = intent.getAction();
            if(intent != null && "backToUserFrag".equals(action)){
                radioGroup.check(R.id.usr_rv);
            }else if(intent != null && "backToHome".equals(action)){
                radioGroup.check(R.id.home_rd);
            }else if(intent != null && "backToCate".equals(action)){
                radioGroup.check(R.id.cate_rb);
            }else if(intent != null && "backToCar".equals(action)){
                radioGroup.check(R.id.car_rb);
            }else if(intent != null && "backToComm".equals(action)){
                radioGroup.check(R.id.comm_rb);
            }

        }



    }

    public void selectHome(){
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.home_rd:
                        fragChose = 0;
                        break;
                    case R.id.cate_rb:
                        fragChose = 1;
                        break;
                    case R.id.comm_rb:
                        fragChose = 2;
                        break;
                    case R.id.car_rb:
                        fragChose = 3;
                        break;
                    case R.id.usr_rv:
                        fragChose = 4;
                        break;
                }
                if(nowFrag != fragChose){
                    getSupportFragmentManager().beginTransaction().replace(R.id.frag_position,frags.get(fragChose)).commit();
                    nowFrag = fragChose;
                }

            }
        });
    }

    //初始化fragment
    public List<Fragment> initfrag(){
        List<Fragment> frag_list = new ArrayList<>();
        HomeOutFragment homeOutFragment = new HomeOutFragment();
        CateFragment cateFragment = new CateFragment();
        CommFragment commFragment = new CommFragment();
        ShoppingCarFragment shoppingCarFragment = new ShoppingCarFragment();
        UsrFragment usrFragment = new UsrFragment();

        frag_list.add(homeOutFragment);
        frag_list.add(cateFragment);
        frag_list.add(commFragment);
        frag_list.add(shoppingCarFragment);
        frag_list.add(usrFragment);

        return frag_list;
    }

}